<?php

namespace App\Util;

use Phalcon\Http\Response;

class Fun
{
	public function echoJson($data,$code = 0){
		$code = (int)$code;
		$content['code'] = $code;
		$key = $code || is_string($data) ? 'msg' : 'data';
		$content[$key] = $data;
		$response = new Response();
		$response->setJsonContent($content);
		$response->send();
		exit;
	}
	
	public function db_execute($sql){
		$data = $this->postCurl('http://127.0.0.1:9509',["sql" => $sql]);
		return json_decode($data);
	}

	public function postCurl($url,$data = []) {
        $ch = curl_init ();
		curl_setopt ($ch,CURLOPT_URL, $url);
		curl_setopt ($ch,CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
		curl_setopt ($ch,CURLOPT_HTTPHEADER, ['Expect:']);
		curl_setopt ($ch,CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
		curl_setopt ($ch,CURLOPT_POST, 1);
		curl_setopt ($ch,CURLOPT_HEADER, 0);
		curl_setopt ($ch,CURLOPT_RETURNTRANSFER, 1);
		curl_setopt ($ch,CURLOPT_POSTFIELDS, http_build_query($data));
		$return = curl_exec($ch);
		curl_close($ch);
		return $return;
	}

	public function postJsonCurl($url,$data = [],$_header = []) {
		$post_json = json_encode($data);
		$header = [
			'Accept: application/json',
			'Content-Type: application/json;charset=utf-8',
			'Content-Length: ' . strlen($post_json),
		];
		$header = array_merge($header,$_header);
        $ch = curl_init ();
		curl_setopt($ch,CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
		curl_setopt($ch,CURLOPT_POST, 1);
		curl_setopt($ch,CURLOPT_HEADER, 0);
		curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch,CURLOPT_POSTFIELDS, $post_json);
		curl_setopt($ch,CURLOPT_HTTPHEADER, $header);
		$return = curl_exec($ch);
		curl_close($ch);

		return $return;
	}

	public function postCurlSsl(){
		/*$url = 'https://ewzj.local';
		$ch = curl_init();
		curl_setopt($ch,CURLOPT_TIMEOUT,30);
		curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch,CURLOPT_URL,$url);
		curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($ch,CURLOPT_SSL_VERIFYHOST, 0);
		curl_setopt($ch,CURLOPT_VERBOSE, 1);
		curl_setopt($ch,CURLOPT_SSLCERT, getcwd() ."/cert/client-cert.pem");
		curl_setopt($ch,CURLOPT_SSLKEY, getcwd(). "/cert/client-key.pem");
		$data = curl_exec($ch);
		curl_close($ch);*/
	}

	/**
	 * [getFullUrl 获取完整url]
	 * @param  [type]  $old_url       [原始url]
	 * @param  boolean $is_https      [是否采用https]
	 * @param  [type]  $self_prefix   [采用自定义前缀]
	 * @return [type]                 [description]
	 */
	public function getFullUrl($old_url, $is_https = FALSE, $self_prefix = NULL)
	{
		$new_url = $old_url;
		$pattern = '|^https?://|';
		$prefix = 'http://';
		if ($is_https)
			$prefix = 'https://';
		if ($self_prefix)
			$prefix = $self_prefix;
    	
    	if(!preg_match($pattern, $old_url))
    		$new_url = $prefix . $old_url;

    	$new_url = str_replace('https://', 'http://', $new_url);

    	if ($is_https)
			$new_url = str_replace('http://', 'https://', $new_url);

    	return $new_url;
	}
    public function getCode($length=32,$mode=0)//获取随机验证码函数
    {
        switch ($mode)
        {
            case '1':
                $str='123456789';
                break;
            case '2':
                $str='abcdefghijklmnopqrstuvwxyz';
                break;
            case '3':
                $str='ABCDEFGHIJKLMNOPQRSTUVWXYZ';
                break;
            case '4':
                $str='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
                break;
            case '5':
                $str='ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
                break;
            case '6':
                $str='abcdefghijklmnopqrstuvwxyz1234567890';
                break;
            default:
                $str='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890';
                break;
        }
        $checkstr='';
        $len=strlen($str)-1;
        for ($i=0;$i<$length;$i++)
        {
            //$num=rand(0,$len);//产生一个0到$len之间的随机数
            $num=mt_rand(0,$len);//产生一个0到$len之间的随机数
            $checkstr.=$str[$num];


        }
        return $checkstr;
    }
    
    //
    public function getAllTextImages($html){
        
        preg_match_all('/background-image:[ ]?url\([\'|"]?(.*?\.(?:png|jpg|jpeg|gif))/is',htmlspecialchars_decode($html),$bg_image_data);
        preg_match_all('/<[img|IMG].*?src=[\'|\"](.*?(?:[\.gif|\.jpg|\.png])(.*?))[\'|\"].*?[\/]?>/is',$html,$image_data);
        $pics_data = array_merge((array)$bg_image_data[1],(array)$image_data[1]);
        return (array)$pics_data;
    }
    
    public function genTree($items){
        foreach ($items as $item){
            $_items[$item->id] = (array)$item;
        }
        $items = $_items;
        unset($_items);
        
        $tree = array(); 
        foreach ($items as $item){
            if (isset($items[$item['pid']]))
                $items[$item['pid']]['son'][] = &$items[$item['id']];
            else
                $tree[] = &$items[$item['id']];
        }
        return $tree;

    }

}
